<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="Description" content="" />

        <title>Spring 3.0就这么简单  -- 熊大书城</title>
        <script type="text/javascript" src="/script/jquery.js"></script>
        <link href="/style/blue/main.css" rel="stylesheet" type="text/css" />
        <script>
            // 使用JavaScript通过Ajax请求后端数据并填充到页面中
            var bookId = getUrlParameter('bookId');

            // 发送Ajax请求获取书籍详情
            $.get("/book/" + bookId, function (data, status) {
                console.log(data);
                console.log(status);
                if (status == "success") {
                    $("#bookTitle").text(data.bname);
                    $("#bookId").text(data.bookNo);
                    $("#bookStock").text(data.stock);
                    $("#bookWeight").text(data.weight);
                    $("#bookPublishTime").text(data.publishtime);
                    $("#bookPrice").text("￥" + data.price + "元");
                    $("#bookDescription").text(data.description);
                    $("#bookImage").attr("src", "/style/blue/images/"+data.imageW);
                    initializeTotalPrice(data.price);
                }
            });

            function initializeTotalPrice(price) {
                var quantity = document.getElementById("number").value;
                price = parseFloat(price);
                var totalPrice = quantity * price;
                document.getElementById("totalPrice").innerHTML = "￥" + totalPrice.toFixed(2) + "元";
            }
            function addToCart() {
                var bookId = getUrlParameter('bookId');
                var quantity = $("#number").val();

                // 发送请求获取书籍信息
                $.get("/book/" + bookId, function (data, status) {
                    // 将获取到的书籍信息赋值给 obj
                    var obj = {
                        cartItemId: data.bid,
                        price: data.price,
                        imageB: data.imageB,
                        quantity: parseInt(quantity),
                        bid: parseInt(bookId),
                        uid: null,
                        bname:null
                    };

                    // 发送 obj 到后端
                    $.ajax({
                        type: "POST",
                        url: "/book/addToCart",
                        contentType: "application/json",
                        data: JSON.stringify(obj),
                        success: function (response) {
                            console.log(obj);
                            alert("添加购物车成功");
                            window.location.href = "/common/cart.html";
                        },
                        error: function (error) {
                            console.error("加入购物车失败->错误信息如下: " + JSON.stringify(error));
                        }
                    });
                });
            }


            // 解析URL参数函数
            function getUrlParameter(name) {
                name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
                var regex = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)');
                var results = regex.exec(location.search);
                return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
            }

        </script>
    </head>
    <body style="cursor: auto;">
        <div class="block clearfix" style="position: relative; height: 98px;">
            <a href="#" name="top">
                <img class="logo" src="/style/blue/images/logo_max.png" />
            </a>
            <div id="topNav" class="clearfix">
                <div style="float: left;">
                    <font id="ECS_MEMBERZONE"><div id="append_parent"></div>
                        欢迎光临本店&nbsp;
                        <a href="#"> 登录</a>
                        <a href="#">注册</a>
                    </font>
                </div>
                <div style="float: right;">
                    <a href="#">查看购物车</a>
                </div>
            </div>
            <div id="mainNav" class="clearfix">
                <a href="#">全部分类<span></span></a>
                <a href="#"  class="cur">JavaSE分类<span></span></a>
                <a href="#">JavaEE分类<span></span></a>
                <a href="#">JavaScript分类<span></span></a>
            </div>
        </div>

        <div class="header_bg">

            <div style="float: left; font-size: 14px; color:white; padding-left: 15px;">
            </div>

            <form id="searchForm" method="get" >
                <input name="keywords" id="keyword" type="text" />
                <input name="imageField" value=" " class="go" style="cursor: pointer; background: url('/style/blue/images/sousuo.gif') no-repeat scroll 0% 0% transparent; width: 39px; height: 20px; border: medium none; float: left; margin-right: 15px; vertical-align: middle;" type="submit" />
            </form>
        </div>
        <div class="blank5"></div>
        <div class="blank"></div>
        <div class="block clearfix">
            <div class="AreaL">
                <h3><span>商品分类</span></h3>
                <div id="category_tree" class="box_1">
                    <dl>
                        <dt><a href="#">全部分类</a></dt>
                        <dd>       </dd>
                    </dl>
                    <dl>
                        <dt><a href="#">JavaSE分类</a></dt>
                        <dd>       </dd>
                    </dl>
                    <dl>
                        <dt><a href="#">JavaEE分类</a></dt>
                        <dd>       </dd>
                    </dl>
                    <dl>
                        <dt><a href="#">JavaScript分类</a></dt>
                        <dd>       </dd>
                    </dl>
                </div>
                <div class="blank"></div>

                <div style="display: block;" class="box" id="history_div">
                    <h3><span>浏览历史</span></h3>
                    <div class="box_1">

                        <div class="boxCenterList clearfix" id="history_list">
                            <ul class="clearfix">
                                <li class="goodsimg">
                                    <a href="#" target="_blank">
                                        <img src="/style/blue/images/book_img/23239786-1_b.jpg" alt="Java虚拟机并发编程" class="B_blue" />
                                    </a>
                                </li>
                                <li>
                                    <a href="#" target="_blank" title="Java虚拟机并发编程">Java虚拟机并发编程</a><br />
                                    本店售价：<font class="f1">￥40.70元</font><br />
                                </li>
                            </ul>
                            <ul class="clearfix">
                                <li class="goodsimg">
                                    <a href="#" target="_blank">
                                        <img src="/style/blue/images/book_img/22757564-1_b.jpg" alt="基于MVC的JavaScript" class="B_blue" />
                                    </a>
                                </li>
                                <li>
                                    <a href="#" target="_blank" title="基于MVC的JavaScript">基于MVC的JavaScript</a><br />
                                    本店售价：<font class="f1">￥40.70元</font><br />
                                </li>
                                <!-- 总价显示位置 -->

                            </ul><ul id="clear_history"><a onclick="clear_history()">[清空]</a></ul>  </div>

                    </div>
                </div>
                <div class="blank5"></div>
            </div>

            <div class="AreaR">
                <div id="goodsInfo" class="clearfix">
                    <div class="imgInfo">
                            <img id="bookImage" src="/style/blue/images/book_img/22938474-1_w.jpg" alt="Spring 3.0就这么简单" height="310px" width="310px;" />
                    </div>
                    <div class="textInfo">
                        <form action="javascript:addToCart(9)" method="post" name="ECS_FORMBUY" id="ECS_FORMBUY">
                            <div class="clearfix" style="font-size: 14px; font-weight: bold; padding-bottom: 8px;">
                                <p id="bookTitle"></p>
                            </div>
                            <ul>
                                <li class="clearfix">
                                    <dd>
                                        <strong>商品货号：</strong><font class="shop" id="bookId"></font><br />
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>商品库存：</strong><font class="shop" id="bookStock"></font> 件<br />
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>商品重量：</strong><font class="shop" id="bookWeight"></font> 克<br />
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>上架时间：</strong><font class="shop" id="bookPublishTime"></font><br />
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>本店售价：</strong><font class="shop" id="bookPrice">￥40.70元</font><br />
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>用户评价：</strong>
                                        <img src="/style/blue/images/stars3.gif" alt="comment rank 5">
                                    </dd>
                                </li>
                                <li class="clearfix">
                                    <dd>
                                        <strong>购买数量：</strong>
                                        <input name="number" id="number" value="1" size="4" onblur="changePrice()" style="border: 1px solid rgb(204, 204, 204);" type="text" />
                                    </dd>
                                </li>
                                <li class="padd">
                                    <a href="javascript:addToCart()"><img src="/style/blue/images/shoucang2.gif"></a>
                                    <a href="#"><img src="/style/blue/images/goumai2.gif"></a>
                                </li>
                            </ul>
                            <div id="totalPrice1">总价：<font class="shop" id="totalPrice"></font></div>

                        </form>
                    </div>
                </div>
                <div class="blank"></div>
                <div class="box">
                    <div class="box_1">
                        <h3 style="padding: 0pt 5px;">
                            <div id="com_b" class="history clearfix">
                                <h2 style="cursor: pointer;">商品描述：</h2>
                            </div>
                        </h3>
                        <div id="com_v" class="boxCenterList RelaArticle">
                            <p id="bookDescription">Spring 3.0就这么简单$（国内原创的Java敏捷开发图书，展现作者Spring原创开源项目ROP开发的全过程，所有项目工程均以Maven组织）</p>
                            <p>&nbsp;</p>
                        </div>
                <div class="blank"></div>
                   </div>
            </div>

        </div>
        <div class="blank"></div>
        <iframe src="footer.html" frameborder="0" scrolling="no" width="100%" height="300"></iframe>
            <script type="text/javascript">

                // 修改价格函数
                function changePrice(price) {
                    var quantity = document.getElementById("number").value;
                    price = parseFloat(price);
                    var totalPrice = quantity * price;
                    document.getElementById("totalPrice").innerHTML = "￥" + totalPrice.toFixed(2) + "元";
                }

                //更新总价
                document.getElementById("number").addEventListener("input", function () {
                    changePrice($("#bookPrice").text().replace("￥", "").replace("元", ""));
                });


                $("#totalPrice1").blur(function () {
                    changePrice($("#bookPrice").text().replace("￥", "").replace("元", ""));
                })

                document.body.addEventListener("click", function (event) {
                    var target = event.target;
                    if (target.id !== "number") {
                        changePrice($("#bookPrice").text().replace("￥", "").replace("元", ""));
                    }
                });

                // 初始化总价
                $(document).ready(function () {
                    changePrice($("#bookPrice").text().replace("￥", "").replace("元", ""));
                });
            </script>
    </body>
</html>